package com.lcf.system.controller;

import com.lcf.common.utils.UserUtils;
import com.lcf.framework.annotation.PermissionCheck;
import com.lcf.framework.vo.PageVO;
import com.lcf.system.dto.user.SysUserDTO;
import com.lcf.system.dto.user.SysUserEditDTO;
import com.lcf.system.dto.user.SysUserPasswordDTO;
import com.lcf.system.query.user.SysUserPageQuery;
import com.lcf.system.service.SysUserService;
import com.lcf.system.vo.user.SysUserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.lcf.framework.enums.PermissionEnum.*;

/**
 * 用户表(User)表控制层
 *
 * @author lcf
 * @since 2025-02-19 13:27:49
 */
@Tag(name = "用户")
@RestController
@RequestMapping("user")
public class SysUserController {
    @Resource
    private SysUserService userService;

    /**
     * 创建用户
     */
    @Operation(summary = "创建用户")
    @PermissionCheck(SYS_USER_ADD)
    @PostMapping
    public void add(@Validated @RequestBody SysUserDTO dto) {
        userService.add(dto);
    }

    /**
     * 修改我的信息
     */
    @Operation(summary = "修改我的信息")
    @PatchMapping
    public void edit(@Validated @RequestBody SysUserEditDTO dto) {
        userService.edit(dto);
    }

    /**
     * 修改我的密码
     */
    @Operation(summary = "修改我的密码")
    @PostMapping("editPassword")
    public void editPassword(@RequestBody@Validated SysUserPasswordDTO dto) {
        userService.editPassword(dto);
    }

    /**
     * 分页查询用户
     */
    @Operation(summary = "分页查询用户")
    @PermissionCheck(SYS_USER_PAGE)
    @GetMapping("page")
    public PageVO<SysUserVO> page(SysUserPageQuery query) {
        return userService.page(query);
    }

    /**
     * 查看用户详情
     */
    @Operation(summary = "查看用户详情")
    @PermissionCheck(SYS_USER_DETAIL)
    @GetMapping("{userId}")
    public SysUserVO getDetail(@PathVariable @Schema(description = "用户id") long userId) {
        return userService.getDetail(userId);
    }

    /**
     * 查看我的信息
     */
    @Operation(summary = "查看我的信息")
    @GetMapping
    public SysUserVO getDetail() {
        return userService.getDetail(UserUtils.getUserId());
    }
}

